package com.baojun.newterritory.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.lang.ref.WeakReference;

/**
 * 微信支付回调
 *
 * @author 刘利强
 * @date 2019/1/16
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    private MyHandler handler;

    private static class MyHandler extends Handler {
        private final WeakReference<WXPayEntryActivity> wxEntryActivityWeakReference;

        public MyHandler(WXPayEntryActivity wxEntryActivity) {
            wxEntryActivityWeakReference = new WeakReference<WXPayEntryActivity>(wxEntryActivity);
        }
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        api = WXAPIFactory.createWXAPI(this, "wxedfa5d9c6ed20a47", false);
        handler = new MyHandler(this);

        try {
            Intent intent = getIntent();
            api.handleIntent(intent, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        //如果是微信支付
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            /*LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(WxPayResultReceiver.ACTION));
//            finish();*/
//            Intent intent = new Intent(this, CommonWebActivity.class);
//            intent.putExtra("errorCode", baseResp.errCode);
//            startActivity(intent);
        }
    }
}
